<?php

namespace qore_base\cli;
use \qore\base\ControllerDispatcher;
use \Qore;

/**
 * @author Samuel Nogueira <samuel.nogueira.dev@gmail.com>
 */
class CliControllerDispatcher extends ControllerDispatcher {
	public $rules;
	protected $_controller;

	/**
	 * Returns controller based on context variables
	 *
	 * @return \qore\base\Controller
	 */
	public function getController() {
		$class = Qore::app()->getArgument(array('controller', 'c'), $this->getDefaultController());
		
		return $class::i();
	}
	
	public function getAction() {
		$action = Qore::app()->getArgument(array('action', 'a', 0));
		$method = 'action'.ucfirst($action);
		return method_exists($this->getController(), $method) ? $action : $this->getDefaultAction();
	}

	/**
	 * Calls appropriate action with the appropriate arguments
	 */
	public function dispatch() {
		$action = $this->getAction();
		$method = 'action'.ucfirst($action);
		echo $this->getController()->$method();
	}
}